package com.zfast.config.service.Impl;

import com.zfast.config.entity.Log;
import com.zfast.config.repository.LogRepository;
import com.zfast.config.service.LogService;
import com.zfast.config.vo.PageBean;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Classname LogServiceImpl
 * @Description None
 * @Date 2019/7/24 12:21
 * @Created by WDD
 */
@Service
public class LogServiceImpl implements LogService {
    @Resource
    LogRepository logRepository;

    @Override
    public void insertByLog(Log log) {
        logRepository.save(log);
    }

    @Override
    public PageBean<Log> queryPage(Map<String, Object> paramMap) {
        PageBean<Log> pageBean = new PageBean<>((Integer) paramMap.get("pageno"), (Integer) paramMap.get("pagesize"));

        Integer pageno = (Integer) paramMap.get("pageno") - 1;
        Integer pagesize = (Integer) paramMap.get("pagesize");

        Page<Log> all = logRepository.findAll(PageRequest.of(pageno, pagesize));

        pageBean.setDatas(all.getContent());
        pageBean.setTotalsize((int) all.getTotalElements());
        return pageBean;
    }

    @Override
    public int delByLogIds(List<String> ids) {
        logRepository.deleteAllByIdInBatch(ids);
        return ids.size();
    }
}
